package com.xxx.nio;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @version v1.0
 * @Description:
 * @Author zhanzhongxin
 * @Date 2021/7/23 14:51
 * @Copyright 博为峰
 */
public class PathsDemo {
    public static void main(String[] args) throws IOException {
//        m1();
//        m2();
//        m3();
//        m4();
//        m5();
        Path path = Paths.get("C:\\Users\\zhanzhongxin\\Desktop\\java\\javase\\a");
        Files.walkFileTree(path, new FileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                return null;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                return null;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                return null;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                return null;
            }
        });
    }

    private static void m5() throws IOException {
        Path path = Paths.get("C:\\Users\\zhanzhongxin\\Desktop\\java\\javase");
        Stream<Path> list = Files.list(path);
        Iterator<Path> iterator = list.iterator();
        while (iterator.hasNext()) {
            Path next = iterator.next();
            System.out.println("next = " + next);
        }
    }

    private static void m4() throws IOException {
        Path path = Paths.get("C:\\Users\\zhanzhongxin\\Desktop\\java\\a.txt");
        Path target = Paths.get("C:\\Users\\zhanzhongxin\\Desktop\\java\\a1.txt");

//        Files.copy(path, target, StandardCopyOption.REPLACE_EXISTING);

        Files.delete(target);
    }

    private static void m3() throws IOException {
        Path path = Paths.get("C:\\Users\\zhanzhongxin\\Desktop\\java\\a.txt");
//        BufferedWriter bufferedWriter = Files.newBufferedWriter(path);
//        bufferedWriter.write("abc");
//        bufferedWriter.close();

        BufferedReader bufferedReader = Files.newBufferedReader(path);
        System.out.println(bufferedReader.readLine());
        bufferedReader.close();
    }

    private static void m2() throws IOException {
        Path path = Paths.get("C:\\Users\\zhanzhongxin\\Desktop\\java\\a.txt");
//        Files.createFile(path);

        Path dir = Paths.get("C:\\Users\\zhanzhongxin\\Desktop\\java\\a");
//        Files.createDirectories(dir);

        Map<String, Object> stringObjectMap = Files.readAttributes(path, "*");
//        System.out.println(stringObjectMap);
        //{lastAccessTime=2021-07-23T07:19:01.067591Z,
        // lastModifiedTime=2021-07-23T07:19:01.067591Z,
        // size=0, creationTime=2021-07-23T07:19:01.067591Z,
        // isSymbolicLink=false, isRegularFile=true,
        // fileKey=null, isOther=false, isDirectory=false}
        System.out.println(Files.getLastModifiedTime(path));//最后修改时间
        System.out.println(Files.isDirectory(path));//是否是目录
        System.out.println(Files.isHidden(path));//是否是隐藏文件
        System.out.println(Files.isReadable(path));//是否是只读文件
        System.out.println(Files.size(path));//文件大小
    }

    private static void m1() {
        Path path = Paths.get("C:\\Users\\zhanzhongxin\\Desktop\\java\\IO.jpg");
        //Path-->File
        path.toFile();

        File file = new File("C:\\Users\\zhanzhongxin\\Desktop\\java\\IO.jpg");
        //File-->Path
        file.toPath();
    }

    /*
       1.使用nio创建a/b/c文件夹
       2.遍历a/b/c文件夹的同时在对应的文件夹下创建以文件夹名为名称的文件，并写入信息（abc）
     */
}
